package com.wyd.wenyongdaAdmin.xo.admin.role.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyd.wenyongdaAdmin.commons.model.admin.role.dto.RoleDTO;
import com.wyd.wenyongdaAdmin.commons.model.admin.role.entity.RoleEntity;
import com.wyd.wenyongdaAdmin.xo.admin.role.mapper.RoleMapper;
import com.wyd.wenyongdaAdmin.xo.admin.role.service.IRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements IRoleService {

    private static final Logger log = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Override
    public Integer addRole(RoleDTO roleDTO) {
        if (ObjectUtil.isNotNull(roleDTO)) {
            RoleEntity role = new RoleEntity();
            BeanUtil.copyProperties(roleDTO, role);
            return this.baseMapper.insert(role);
        } else {
            return 0;
        }
    }

    @Override
    public Integer updateRole(RoleDTO roleDTO) {
        if (ObjectUtil.isNotNull(roleDTO)) {
            RoleEntity role = new RoleEntity();
            BeanUtil.copyProperties(roleDTO, role);
            return this.baseMapper.updateById(role);
        } else {
            return 0;
        }
    }

    @Override
    public Integer deleteRole(List<Long> ids) {
        if (ids.size() > 0) {
            return this.baseMapper.deleteBatchIds(ids);
        } else {
            return 0;
        }
    }

    @Override
    public PageInfo<RoleDTO> selectRolePageWithCount(Integer pageNumber, Integer pageSize, RoleDTO role) {
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<RoleEntity>().orderBy(true, true, "sort");
        if (ObjectUtil.isNotNull(role)) {
            if (StrUtil.isNotBlank(role.getName())) {
                queryWrapper.like("name", role.getName());
            }
            if (StrUtil.isNotBlank(role.getCode())) {
                queryWrapper.like("code", role.getCode());
            }
            if (ObjectUtil.isNotNull(role.getIsEnabled())) {
                queryWrapper.eq("is_enabled", role.getIsEnabled());
            }
        }
        if (pageNumber != 0 && pageSize != 0) {
            PageHelper.startPage(pageNumber, pageSize);
            List<RoleEntity> roleEntityList = this.baseMapper.selectList(queryWrapper);
            PageInfo roleDTOPageInfo = new PageInfo<>(roleEntityList);
            List<RoleDTO> roleDTOList = new ArrayList<>();
            RoleDTO roleDTO = null;
            for (RoleEntity roleEntity : roleEntityList) {
                roleDTO = new RoleDTO();
                BeanUtil.copyProperties(roleEntity, roleDTO);
                roleDTOList.add(roleDTO);
            }
            roleDTOPageInfo.setList(roleDTOList);
            PageHelper.clearPage();
            return roleDTOPageInfo;
        } else {
            return null;
        }
    }

    @Override
    public List<RoleDTO> selectAllRole(Integer isEnabled) {
        List<RoleEntity> roleEntityList = this.baseMapper.selectList(
                new QueryWrapper<RoleEntity>()
                        .eq("is_enabled", isEnabled)
                        .orderBy(true, true, "sort"));
        List<RoleDTO> roleDTOList = new ArrayList<>();
        RoleDTO roleDTO = null;
        for (RoleEntity roleEntity : roleEntityList) {
            roleDTO = new RoleDTO();
            BeanUtil.copyProperties(roleEntity, roleDTO);
            roleDTOList.add(roleDTO);
        }
        return roleDTOList;
    }

}
